<?php
/**
 * 个人网址文件夹
 */
namespace APP\M;

class Cata extends Model
{
    const T_CATA = 'user_url_cata';

    public function init()
    {
        if (!$this->_email) {
            throw new Exception("文件夹操作：用户标识丢失");
        }
    }

    /**
     * 获取某用户的分类
     */
    public function getList()
    {
        $sql = 'SELECT id, name, url_cnt FROM '. self::T_CATA .' WHERE email="'. $this->_email .'" ORDER BY url_cnt DESC';
        $data = $this->getDb()->fetchAll($sql);
        $result = array();
        foreach ($data as $item) {
            $result[$item['id']] = $item;
        }
        return $result;
    }

    /**
     * 获取文件夹总数
     */
    public function getCount()
    {
        $sql = 'SELECT COUNT(*) FROM '. self::T_CATA .' WHERE email="'. $this->_email .'"';
        return $this->getDb()->fetchOne($sql);
    }

    /**
     * 更新分类统计数
     */
    public function upCount($id, $oid = 0)
    {
        if (!$id) {
            return;
        }
        if ($id != $oid) {
            // 旧的先减
            if ($oid) {
                $sql = 'UPDATE '. self::T_CATA .' SET url_cnt=url_cnt-1 WHERE id='. $oid;
                $this->getDb()->query($sql);
            }
            // 新的加
            $sql = 'UPDATE '. self::T_CATA .' SET url_cnt=url_cnt+1 WHERE id='. $id;
            $this->getDb()->query($sql);
        }
    }


    /**
     * 创建
     */
    public function create($name)
    {
        $id = $this->isCataExists($name, 'name');
        if ($id) {
            return $id;
        } else {
            $this->getDb()->insert(self::T_CATA, array(
                'email' => $this->_email,
                'name' => $name,
            ));
            return $this->getLastId();
        }
    }

    /**
     * 获取最新id
     */
    public function getLastId()
    {
        return $this->getDb()->fetchOne('SELECT last_insert_id()');
    }

    /**
     * 修改
     */
    public function edit($id, $name)
    {
        if ($this->isCataExists($id)) {
            $this->getDb()->update(self::T_CATA, array('name'=>$name), array('id=?'=>$id));
            return self::S_SUCCESS;
        } else {
            return self::S_NOT_EXISTS;
        }
    }


    /**
     * 如果force为1则强制删除，同时删除该分类下的所有网址
     * 如果force为0则如果该分类下没有网址才能删除
     */
    public function delete($val, $type = 'id', $force = 0)
    {
        $sql = 'SELECT url_cnt FROM '. self::T_CATA .' WHERE '. $this->byWhere($val, $type);
        if ($this->getDb()->fetchOne($sql)) {
            if ($force) {
                // 强制删除，同时删除对应的url
                $cid = $type == 'id' ? $val : $this->getDb()->fetchOne('SELECT id FROM '. self::T_CATA .' WHERE '. $this->byWhere($val, $type));
                $this->getModel('url')->deleteByCid($cid);
            } else {
                return self::S_NOT_EMPTY;
            }
        }
        // 最后删除文件夹
        $this->getDb()->delete(self::T_CATA, $this->byWhere($val, $type, 'array'));
    }


    /**
     * 是否存在，按id或按name判断
     */
    public function isCataExists($val, $type = 'id')
    {
        $sql = 'SELECT id FROM '. self::T_CATA .' WHERE '. $this->byWhere($val, $type);
        return $this->getDb()->fetchOne($sql);
    }


    /**
     * 产生按id或按name的条件
     */
    public function byWhere($val, $type = 'id', $return = 'string')
    {
        if ($return == 'string') {
            return ($type == 'id') ? 'id='. (int)$val : 'email="'. $this->_email .'" AND name="'. $val .'"';
        } else {
            return ($type == 'id') ? array('id=?'=>$val) : array('email=?'=>$this->_email, 'name=?'=>$val);
        }
    }
}